<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="模型名称" prop="name">
        <el-input v-model.trim="dataForm.name" placeholder="请输入3-100个字符"></el-input>
      </el-form-item>
      <el-form-item label="模型分类" prop="category">
        <el-input v-model="dataForm.category" placeholder="请输入分类"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input  type="textarea" v-model="dataForm.desc" placeholder="请描述"></el-input>
      </el-form-item>
      <el-form-item label="模型标识" prop="key">
        <el-input  type="textarea" v-model="dataForm.key" placeholder="请描述"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          name: '',
          category: '',
          desc: '',
          key: ''
        },
        loading: false,
        dataRule: {
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      async initEditor(id) {
        this.dataForm = {
          name: '',
          category: '',
          desc: '',
          key: ''
        }
        this.$nextTick(async () => {
          this.$refs['dataForm'].resetFields()
          if (id) {
            const res = await this.$http({
              url: this.$http.adornUrl(`/act/model/deploy/${id}`),
              method: 'post'
            })
            console.log(res)
            if (res.code === 200) {
              this.dataForm = res.data
            }
          }
        })
        this.visible = true
      },
      // 表单提交
      async dataFormSubmit() {
        try {
          var isValid = await this.$refs['dataForm'].validate()
          if (!isValid) return
          this.loading = true
          let res = await this.$http({
            url: this.$http.adornUrl(`/act/model/insert`),
            method: 'post',
            data: {
              name: this.dataForm.name,
              category: this.dataForm.category,
              desc: this.dataForm.desc,
              key: this.dataForm.key
            }
          })

          if (res && res.code === 200) {
            this.visible = false
            this.$notify.success({ title: '成功', message: '保存成功' })
            this.$emit('refreshDataList')
            this.$refs.ckEditor && this.$refs.ckEditor.setData('')
          } else {
            this.$notify.error({ title: '错误', message: res.msg })
          }
          this.loading = false
        } catch (err) {
          this.loading = false
        }
      }
    }
  }
</script>

<style scoped>
</style>
